package com.ihelp.controller;

import com.ihelp.entities.CommonResult;
import com.ihelp.pojo.Article;
import com.ihelp.pojo.HospitalDepartment;
import com.ihelp.service.ArticleService;
import com.ihelp.service.HospitalDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-06-03
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Value("${ihelp.path.article}")
    public String articleRoutePrefix;
    @Autowired
    private ArticleService articleService;

    @GetMapping("/all")
    public CommonResult getAllArticle() {
        List<Article> allArticle = articleService.getAllArticle();
        return new CommonResult(200, "获取所有文章成功", allArticle);
    }

    @GetMapping("/id/{id}")
    public CommonResult getArticleById(@PathVariable Integer id) throws IOException {
        try {
            Article article = articleService.getArticleById(id);
            if (article != null) {
                String fileName = article.getFileName();
                Path path = Paths.get(articleRoutePrefix + fileName);
                String content = Files.readString(path);
                article.setContent(content);
                return new CommonResult<>(200, "根据id:" + id + ",获取文章成功", article);
            } else {
                return new CommonResult<>(204, "根据id:" + id + ",未找到文章");
            }
        } catch (IOException e) {
            return new CommonResult<>(204, "id:" + id + ",未找到文章文件.txt");
        }
    }

    @PostMapping("/create")
    public CommonResult createArticle(@RequestParam(required = false) String author, String context) throws IOException {
        try {
            if (author == null) {

            }
//            文件路径
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filename = uuid + ".txt";
            String pathString = articleRoutePrefix + filename;
            Path path = Paths.get(pathString);
            Files.writeString(path, context);
            //日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = new Date();
            String currentDateStr = sdf.format(currentDate);

            articleService.createArticle(filename, 1, currentDateStr, author);
//            获取id
            List<Article> allArticle = articleService.getArticleByDynamicConditions(null, filename, null, currentDateStr, author);
            Integer articleId = null;
            for (Article article : allArticle) {
                articleId = article.getId();
            }
            return new CommonResult<>(200, "创建文章成功,文章id为："+articleId);

        } catch (IOException e) {
            return new CommonResult<>(400, "写入文章文件.txt失败");
        }
    }

}
